﻿using CCSFeederCameraUI2.Data;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CCSFeederCameraUI2.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class BarcodeController : ControllerBase
    {
        public BarcodeController() { }
        [HttpGet]
        public IActionResult FindAsync([FromQuery] int position)
        {
            try
            {
                List<string> resultStrs = new List<string>();
                using var mdb = new MDbContext();
                string partNum = mdb.MParams.FirstOrDefault(p => p.Name == "PartNum").Value;
                for (int i = 0; i < 4; i++)
                {
                    if (bool.Parse(mdb.MParams.FirstOrDefault(p => p.Name == "ProductUse" && p.PartNum == partNum && p.Hand == i).Value))
                    {
                        var _barcode = mdb.MBarcodes.FirstOrDefault(bar => bar.ItemNo == position + 1 && bar.ProductIndex == i);
                        resultStrs.Add(_barcode.Barcode);
                    }
                    else
                    {
                        resultStrs.Add(string.Empty);
                    }
                }
                return Ok(string.Join("|", resultStrs));
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                return Ok("Error");
            }
        }
        [HttpGet("")]
        public IActionResult Connect()
        {            
            return Ok("OK");
        }
    }
}
